package com.example.home;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 * 
 * @see SystemUiHider
 */
public class MainActivity extends Activity {

	public class TypeListAdapter extends ArrayAdapter<StatisticType> {

		private ArrayList<StatisticType> items;

		public TypeListAdapter(Context context, int textViewResourceId,
				ArrayList<StatisticType> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.statistic_type, null);

			}

			String type = items.get(position).getType();
			if (type != null) {
				TextView typeTextView = (TextView) v
						.findViewById(R.id.type_name);
				if (typeTextView != null) {
					typeTextView.setText(type);
				}
			}

			String value = items.get(position).getUrl();
			if (value != null) {
				WebView valueWebView = (WebView) v
						.findViewById(R.id.type_value);
				if (valueWebView != null) {
					valueWebView.setWebViewClient(new WebViewClient());
					valueWebView.loadUrl(value);
				}
			}
			return v;
		}
	}
	
	EditText priceBox;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		final ListView typeItemsList = (ListView) findViewById(R.id.typeList);
		final Button incomeButton = (Button) findViewById(R.id.incomeButton);
		final Button outcomeButton = (Button) findViewById(R.id.outcomeButton);
		priceBox = (EditText) findViewById(R.id.priceEditBox);
		
		priceBox.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				typeItemsList.setVisibility(View.VISIBLE);
				
			}
		});

		incomeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				try {
					if (priceBox != null && priceBox.getText().toString().length() > 0) {
						Intent intent = new Intent(MainActivity.this,
								TypeActivity.class);
						intent.putExtra("value", priceBox.getText().toString());
						intent.putExtra("type", "income");
						startActivityForResult(intent, 1);
					}
				} catch (Exception ex) {
					Toast toast = Toast.makeText(MainActivity.this, "Грешка: Записът НЕ беше направен!", Toast.LENGTH_SHORT);
					toast.show();
				}

			}
		});

		outcomeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try
				{
					if (priceBox != null) {
						if (priceBox.getText().toString().length() > 0) {
							Intent intent = new Intent(MainActivity.this,
									TypeActivity.class);
							intent.putExtra("value", priceBox.getText().toString());
							intent.putExtra("type", "outcome");
							startActivityForResult(intent, 0);
						}
					}
				}catch (Exception ex)
				{
					Toast toast = Toast.makeText(MainActivity.this, "Грешка: Записът НЕ беше направен!", Toast.LENGTH_SHORT);
					toast.show();
				}
			}
		});

		TypeListAdapter typeArrayAdapter = new TypeListAdapter(
				MainActivity.this, R.layout.statistic_type,
				new ArrayList<StatisticType>());

		typeItemsList.setAdapter(typeArrayAdapter);

//		typeArrayAdapter.add(new StatisticType("ДНЕВЕН БЮДЖЕТ (ЛВ.)",
//				Commands.getDailyBudget));
	
		
		// The topmost view in the activity
		final View root = findViewById(R.id.mainContainer);
		 
		// Detect softkeyboard show / hide. If the visible frame is < 1/3 screen height, seems likely
		// the soft keyboard is with us. Announce changes on the event bus.
		//
		root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
		  @Override public void onGlobalLayout() {
		    Rect r = new Rect();
		    //r will be populated with the coordinates of your view that area still visible.
		    root.getWindowVisibleDisplayFrame(r);
		 
		    int screenHeight = root.getRootView().getHeight();
		    int heightDiff = screenHeight - (r.bottom - r.top);
		    boolean visible = heightDiff > screenHeight / 3;
		 
		    if(!visible)
		    {
		    	typeItemsList.setVisibility(View.INVISIBLE);
		    }
		  }
		});

	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(priceBox != null)
		{
			priceBox.setText("");
		}
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
	}
}
